{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0679a213-c3a1-443b-bb61-98b5c7dd980c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Ch03-5 Test Writing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "09272938-f7b9-44f8-8d02-a62b654032b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Libraries\n",
    "import unittest\n",
    "from Bio.Align import PairwiseAligner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "95af1088-176c-404e-b1bd-ef19e7fbd11d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "FF\n",
      "======================================================================\n",
      "FAIL: test_global_alignment (__main__.TestPairwiseAligner.test_global_alignment)\n",
      "Test global alignment.\n",
      "----------------------------------------------------------------------\n",
      "Traceback (most recent call last):\n",
      "  File \"/var/folders/53/kmyyy3057lndfb0bpwx_2pkr0000gn/T/ipykernel_37172/1953800863.py\", line 24, in test_global_alignment\n",
      "    self.assertAlmostEqual(best_alignment.score, expected_score, places=1)\n",
      "AssertionError: 6.0 != 9.0 within 1 places (3.0 difference)\n",
      "\n",
      "======================================================================\n",
      "FAIL: test_local_alignment (__main__.TestPairwiseAligner.test_local_alignment)\n",
      "Test local alignment.\n",
      "----------------------------------------------------------------------\n",
      "Traceback (most recent call last):\n",
      "  File \"/var/folders/53/kmyyy3057lndfb0bpwx_2pkr0000gn/T/ipykernel_37172/1953800863.py\", line 39, in test_local_alignment\n",
      "    self.assertAlmostEqual(best_alignment.score, expected_score, places=1)\n",
      "AssertionError: 7.0 != 6.0 within 1 places (1.0 difference)\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 2 tests in 0.005s\n",
      "\n",
      "FAILED (failures=2)\n"
     ]
    }
   ],
   "source": [
    "# Define the test Class\n",
    "class TestPairwiseAligner(unittest.TestCase):\n",
    "\n",
    "    def setUp(self):\n",
    "        \"\"\"Set up test cases with sequences and aligner.\"\"\"\n",
    "        self.seq1 = \"ACGTGCTAGCTAG\"\n",
    "        self.seq2 = \"ACGTCGATGCTA\"\n",
    "        self.aligner = PairwiseAligner()\n",
    "        self.aligner.match_score = 1\n",
    "        self.aligner.mismatch_score = -1\n",
    "        self.aligner.open_gap_score = -1\n",
    "        self.aligner.extend_gap_score = -0.5\n",
    "\n",
    "    def test_global_alignment(self):\n",
    "        \"\"\"Test global alignment.\"\"\"\n",
    "        alignments = self.aligner.align(self.seq1, self.seq2)\n",
    "        best_alignment = alignments[0]\n",
    "        \n",
    "        # Expected results\n",
    "        expected_score = 9.0\n",
    "        expected_target = \"ACGTGCTAGCTAG\"\n",
    "        expected_query = \"ACGTCGATGCTA-\"\n",
    "        \n",
    "        self.assertAlmostEqual(best_alignment.score, expected_score, places=1)\n",
    "        self.assertEqual(str(best_alignment).splitlines()[0], \"target            0 ACGTGCTAGCTAG 13\")\n",
    "        self.assertEqual(str(best_alignment).splitlines()[2], \"query             0 ACGTCGATGCTA- 12\")\n",
    "\n",
    "    def test_local_alignment(self):\n",
    "        \"\"\"Test local alignment.\"\"\"\n",
    "        self.aligner.mode = 'local'\n",
    "        alignments = self.aligner.align(self.seq1, self.seq2)\n",
    "        best_alignment = alignments[0]\n",
    "        \n",
    "        # Expected results\n",
    "        expected_score = 6.0\n",
    "        expected_target = \"GTGCTAG\"\n",
    "        expected_query = \"GTCGATG\"\n",
    "        \n",
    "        self.assertAlmostEqual(best_alignment.score, expected_score, places=1)\n",
    "        self.assertIn(expected_target, str(best_alignment))\n",
    "        self.assertIn(expected_query, str(best_alignment))\n",
    "\n",
    "# Run the tests\n",
    "if __name__ == \"__main__\":\n",
    "    unittest.main(argv=[''], exit=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6793ddb1-4ca7-4915-b9a1-f2e81d939e8f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
